#include "file.h"
#include "epoll_loop.h"
#include "task.h"
#include <span>
#include <sys/epoll.h>
#include <unistd.h>

namespace cobt {

FileHandle::FileHandle(int fd) : m_fd(fd) {}
FileHandle::FileHandle(FileHandle &&that) : m_fd(that.m_fd) { that.m_fd = -1; }
FileHandle &FileHandle::operator=(FileHandle &&that) {
  m_fd = that.m_fd;
  that.m_fd = -1;
  return *this;
}
FileHandle::~FileHandle() {
  if (m_fd != -1) {
    close(m_fd);
  }
}
int FileHandle::get_fd() const noexcept { return m_fd; }
void FileHandle::release() noexcept { m_fd = -1; }

Task<int> FileHandle::read(std::span<char> buf,
                           std::chrono::milliseconds timeout) {
  return read_async(m_fd, buf, timeout);
}

Task<int> FileHandle::write(std::span<char> buf,
                            std::chrono::milliseconds timeout) {
  return write_async(m_fd, buf, timeout);
}

} // namespace cobt